const jm = require("./aes.js");

//  硬件方提供的key
// var key = "7e834455867c56a9e4757f595e147968";
// 加密
function encryptionData(data, key) {
	console.log(key,'密钥');
	var byteKey = jm.CryptoJS.enc.Hex.parse(key);
	var byteData = jm.CryptoJS.enc.Hex.parse(data);
	var encrypt = jm.CryptoJS.AES.encrypt(byteData, byteKey, {
		mode: jm.CryptoJS.mode.ECB,
		padding: jm.CryptoJS.pad.NoPadding
	});
	var encryptedStr = encrypt.ciphertext.toString();
	return encryptedStr;
}
//解密
function decryptData(data, key) {
	var byteKey = jm.CryptoJS.enc.Hex.parse(key);
	var byteData = jm.CryptoJS.enc.Hex.parse(data);
	byteData = jm.CryptoJS.enc.Base64.stringify(byteData);
	var decrypt = jm.CryptoJS.AES.decrypt(byteData, byteKey, {
		mode: jm.CryptoJS.mode.ECB,
		padding: jm.CryptoJS.pad.NoPadding
	});
	var decryptedStr = decrypt.toString(jm.CryptoJS.enc.Hex);
	return decryptedStr.toString();
}

module.exports = {
	decryptData, //  解密
	encryptionData, //  加密
}